package crp.services.api.graphql.loaders;

import com.qwlabs.lang.C2;
import crp.core.space.app.SpaceService;
import crp.core.space.domain.services.LocationPathLoader;
import crp.services.api.graphql.models.LocationPayload;
import crp.services.api.graphql.models.SpacePayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Source;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;

@GraphQLApi
@RequestScoped
public class SpacePayloadLoader {
    private final SpaceService spaceService;

    @Inject
    public SpacePayloadLoader(SpaceService spaceService) {
        this.spaceService = spaceService;
    }

    public List<@NotNull List<@NotNull LocationPayload>> locationPath(@Source List<SpacePayload> spaces) {
        LocationPathLoader pathLoader = spaceService.pathLoader();
        return C2.list(spaces, space ->
                C2.list(pathLoader.path(space.getLocation().getId()), LocationPayload::of));
    }

    @NotNull
    public List<@NotNull LocationPayload> location(@Source List<SpacePayload> spaces) {
        return C2.list(spaces, space -> LocationPayload.of(space.getLocation()));
    }
}
